;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var dialog = require("dialog");
    var router = require("../hash/pageRouter");
    var validate = require("validate");
    var css_ad = require("./platform_main_advertisement_manager.css");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
    var html_create = require("./admanager-create.html");
    var html_delete = require("./admanager-delete.html");
    var imgSrc;
    
    var init = function(){
		initEvent();
		router.init();
		inquireAdSpaceList();
    };
    
    var initEvent = function(){
    	//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
    	
    	$("#adManage_tbody").on("click", ".detail a", function(){
    		router.route("adDetail/advertisement_detail.html?"+$(this).attr('arg'), true);
    	});
    	
    	$("#add_ad_btn").unbind("click").click(function(){
    		$("#popdialog").html(html_create);
    		dialog.showDialog($("#add_ad_dialog"));
			$("#sureUpdFirst").unbind("click").click( function() {
				newad();
			});
    	});
    	
		//点击删除广告位通知
		$("#main").on("click", ".delete", function() {
			$("#popdialog").html(html_delete);
			var $this = $(this).parents("tr.tr_style");
			var spaceIds = [];
			spaceIds.push($this.attr("spaceId"));
			deletead(spaceIds);
		});
    };
    
	// 点击删除广告调用接口
	var deletead = function(spaceIds) {
		var delete_ad_dialog = $("#delete_ad_dialog");
		dialog.showDialog(delete_ad_dialog, function() {});
		$(".footerBtn").on("click", "#sure_delete", function() {
			base.fnAjax({
				action: "disableAdSpace.json", //接口名称
				oData: {
					spaceIds: spaceIds
				}, //传递参数
				fnSuccess: function(data) { //成功回调
					if (data.status == 200) {
						base.alert(1, "删除成功！");
						router.route("adManage/platform_main_advertisement_manager.html");
					} else {
						base.alert(3, data.message);
					}
					dialog.hideDialog($("#delete_ad_dialog"));
				}
			});
		});
	};
	
	//查询广告位列表
    var inquireAdSpaceList = function(){
        var keyword = $("#keyword").val();
        base.fnAjax({
            action: "inquireAdSpaceList.json",
            async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
            oData: {
            	sessionUuid: base.getData("sid"),
				name: "",
				version: "v0",
				identify: "",
				pageIndex: pageIndex,
				pageSize: pageSize
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                    var tpl = $("#advertise_data_tpl").html();
                    var html = juicer(tpl, data);
                    $("#adManage_tbody").html(html);
                    
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl($("#ad_pagenation"),
                    totalRecordCount,
                    data.pageIndex,
                    data.pageSize,
                    function(p) {
                        pageIndex = p;
                        inquireAdSpaceList();
                    });
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    };
    
    //增加广告
	var newad = function() {
		var promptDialog = validate.validateInput($("#add_ad_dialog"));
		if (promptDialog.length != 0) {
			util.alert(promptDialog);
		} else {
			var body = base.parseForm($("#add_ad_dialog"));
			body.name = $("#newsAddName").val();
			body.description = $("#ad_describe").val();
			body.length = $("#length").val();
			body.width = $("#width").val();
			body.referenceNo = $("#referenceNo").val();
			body.example = imgSrc;
			base.fnAjax({
				action: "createAdSpace.json", //接口名称
				async: false, //同步异步
				nocode: false, //是否要登陆，true为不用登陆（其他页面不用传该参数，默认为false）
				oData: body, //传递参数
				fnSuccess: function(data) { //成功回调
					if (data.status == 200) {
						base.alert(1, "添加成功!");
						base.openUrl("systemManage.html#adManage/platform_main_advertisement_manager.html");
					} else {
						base.alert(3, data.message);
					}
					dialog.hideDialog($("#add_ad_dialog"));
				}
			});
		}
	};
    return {
        init:init
    };
});